/**
 * Created with JetBrains WebStorm.
 * User: Jiyuan
 * Date: 14-5-18
 * Time: 下午12:58
 * To change this template use File | Settings | File Templates.
 */

KindEditor.plugin('formula', function(K) {
    var self = this, name = 'formula';
    self.clickToolbar(name, function() {
        var lang = self.lang(name + '.'),
            html = ['<iframe id="cw_iframe" name="cw_iframe" width="800px" height="380px" src="/Formula/MathFormula" frameborder="no" border="0px" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="yes" >',
                'formula',
                '</iframe>'].join(''),
            dialog = self.createDialog({
                name : name,
                width : 800,
                title : self.lang(name),
                body : html,
                yesBtn : {
                    name : self.lang('yes'),
                    click : function(e) {
                        //alert(window.frames["cw_iframe"].document.getElementById("formulaEditor").innerHTML);
                        var str_formula = $(window.frames["cw_iframe"].document.getElementById("formulaEditor"));

                        var width = H.getObjWidth(str_formula.children().first());
                        var height = H.getObjHeight(str_formula.children().first());
                         str_formula.find(".placeholder").remove();
                        str_formula.find("#fcursor").remove();
                        var mathHtml = '<span math="true" style="position: relative; display: inline-block;width:'+width+';height:'+height+';margin-bottom:-5px;">'+ str_formula.html() +'</span>&nbsp;';

                        //self.appendHtml(mathHtml);

                        self.insertHtml(mathHtml).hideDialog().focus();
                        self.appendHtml("&nbsp;");
                        /*var type = K('.ke-code-type', dialog.div).val(),
                            code = textarea.val(),
                            cls = type === '' ? '' :  ' lang-' + type,
                            html = '<pre class="prettyprint' + cls + '">\n' + K.escape(code) + '</pre> ';*/
                        //if (K.trim(code) === '') {
                        //alert(lang.pleaseInput);
                        //textarea[0].focus();
                        //return;
                        // }
                        //self.insertHtml(html).hideDialog().focus();
                    }
                }
            }),

            textarea = K('textarea', dialog.div);

    });


});